package org.example.service.error;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler;
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
import org.springframework.stereotype.Component;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/5/15 11:17
 */
@Component
public class TestRabbitListenerErrorHandler implements RabbitListenerErrorHandler {

    @Override
    public Object handleError(Message amqpMessage, org.springframework.messaging.Message<?> message,
                              ListenerExecutionFailedException exception) throws Exception {
        // 记录异常信息
        System.err.println("Error occurred while processing message: " + exception.getMessage());
        // 判断异常类型，进行不同的处理
        if (exception.getCause() instanceof SomeSpecificException) {
            // 对于特定异常，可以记录日志或者发送告警，但不重新排队消息
            System.err.println("Special handling for this type of exception.");
            return null; // 返回 null 表示不再重试该消息
        } else {
            // 对于其他异常，可以选择重新排队消息
            throw new ListenerExecutionFailedException("Retrying message due to general failure", exception);
        }
    }
}
